package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.index.LindiIndexQueryVO;
import com.minimalist.common.entity.lindi.vo.index.LindiIndexVO;
import com.minimalist.common.feign.client.lindi.LindiIndexFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_指标管理")
@RequestMapping("/basic/lindi/index")
public class LindiIndexController{

    @Autowired
    private LindiIndexFeignClient feignClient;

    @PostMapping("/addIndex")
    @Operation(summary = "添加指标")
    @SaCheckPermission("lindi:index:add")
    public R<?> addIndex(@RequestBody @Validated(Add.class) LindiIndexVO indexVO){
        indexVO.setCreateId(StpUtil.getLoginIdAsLong());
        indexVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        indexVO.setCreateTime(now);
        indexVO.setUpdateTime(now);
        return feignClient.addIndex(indexVO);
    }

    @DeleteMapping("/deleteIndexByIndexId")
    @Operation(summary = "删除指标 -> 根据指标ID删除")
    @SaCheckPermission("lindi:index:delete")
    public R<?> deleteIndexByIndexId(
            @RequestParam("indexId")
            @NotNull(message = "指标ID不能为空")
            @Parameter(name = "indexId", required = true, description = "指标ID")
            Long indexId){
        return feignClient.deleteIndexByIndexId(indexId);
    }

    @PutMapping("/updateIndexByIndexId")
    @Operation(summary = "修改指标")
    @SaCheckPermission("lindi:index:update")
    public R<?> updateIndexByIndexId(
            @RequestBody @Validated(Update.class) LindiIndexVO indexVO){
        indexVO.setCreateId(StpUtil.getLoginIdAsLong());
        indexVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        indexVO.setCreateTime(now);
        indexVO.setUpdateTime(now);
        return feignClient.updateIndexByIndexId(indexVO);
    }

    @GetMapping("/getPageIndexList")
    @Operation(summary = "查询指标列表(分页)")
    @SaCheckPermission("lindi:index:get")
    public R<PageResp<LindiIndexVO>> getPageIndexList(LindiIndexQueryVO queryVO){
        System.out.println("query: " + queryVO);
        return feignClient.getPageIndexList(queryVO);
    }

    @GetMapping("/getIndexByIndexId/{indexId}")
    @Operation(summary = "根据指标ID查询指标")
    @SaCheckPermission("lindi:index:get")
    public R<LindiIndexVO> getIndexByIndexId(
            @PathVariable(value = "indexId")
            @NotNull(message = "指标ID不能为空")
            @Parameter(name = "IndexId", description = "参数ID", required = true)
            Long indexId){
        return feignClient.getIndexByIndexId(indexId);
    }

    @GetMapping("/getStatistics")
    @Operation(summary = "市储备库指标统计数据")
    @SaCheckPermission("lindi:index:get")
    public R<Map<String, Object>> getStatistics(){
        return feignClient.getStatistics();
    }

    @GetMapping("/getEnableTradeTownNameList")
    @Operation(summary = "获取可交易的镇街名称列表")
    public R<List<String>> getEnableTradeTownNameList(){
        return feignClient.getEnableTradeTownNameList();
    }
}